package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 时尚网
 * @author Persh
 *
 */
public class TrendsNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.trends.com.cn/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://www.trends.com.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]*.shtml",
		"http://www.trends.com.cn/[a-z\\/]+/[0-9\\/]+.shtml",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1#atitle").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1.textt").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("h2").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("title").text();
			if (title.contains("_")){
				title=StringUtils.substringBefore(title, "_");
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td#acontent"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.textw"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.picTxt_2"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.nr"));
				}
			}
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.text"));
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("td#adate_source").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("td.textd").text();
			if(StringUtils.isBlank(authorStr)){
				authorStr=doc.select("div.dat").text();
			}
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");

		date=DateUtils.matchDate(doc.select("time").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("td.textd").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.dat").text());
				if(date==null){
					date=DateUtils.matchDate(url);
				}
			}
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		TrendsNewsAnalyse analyse=new TrendsNewsAnalyse();
		String url="http://www.trends.com.cn/style/fashion/201512/219434.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
